<template>
  <div class="home-page">
    <el-container>
      <!-- 顶部 -->
      <el-header>
        <div class="left-logo">
          <img src="../assets/img/panada.jpeg" alt="" />
          <span>admin</span>
        </div>
        <h3>我的后台管理系统</h3>
        <el-button type="text" @click="logout">退出</el-button>
      </el-header>
      <!-- 内容 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="iscollapse ? '64px' : '200px'">
          <!-- 展开按钮 -->
          <div class="toggleMenu" @click="iscollapse = !iscollapse">|||</div>
          <el-menu
            class="el-menu-vertical-demo"
            background-color="#515458"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="true"
            :collapse="iscollapse"
            :default-active="activeMenue"
            router
          >
            <!-- 导航项 -->
            <el-submenu
              :index="item.id + ''"
              :key="item.id"
              v-for="item in menusList"
            >
              <!-- 菜单项的标题 -->
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 子菜单每一项 -->
              <el-menu-item
                :index="'/' + clildItem.path"
                :key="clildItem.id"
                v-for="clildItem in item.children"
              >
                <i class="el-icon-menu"></i>
                <span>{{ clildItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体 -->
        <el-main>
          <router-view> </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      iconsList: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-isolate',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-icon-',
        145: 'iconfont icon-tongji2x'
      },
      iscollapse: false,
      activeMenue: this.$route.path
    }
  },

  created() {
    this.getMenus()
  },

  methods: {
    logout() {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenus() {
      const res = await this.$axios.get('/menus')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.menusList = res.data
      // console.log(this.menusList)
    }
  }
}
</script>

<style scoped lang='less'>
.el-container,
.el-aside,
.home-page {
  height: 100%;
}
.home-page {
  .el-header {
    h3 {
      text-align: center;
      color: whitesmoke;
    }
    background-color: #313538;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left-logo {
      display: flex;
      align-items: center;
      img {
        user-select: none;
        width: 52px;
        height: 52px;
        border-radius: 50%;
        box-shadow: 0, 0, 13px, background-color #fff;
      }
      span {
        margin-left: 10px;
        color: whitesmoke;
      }
    }
    // .el-button {
    //   color: #999;
    //   .el-button:hover {
    //     color: whitesmoke;
    //   }
    // }
  }

  .el-aside {
    .toggleMenu {
      width: 100%;
      height: 40px;
      background-color: #464747;
      text-align: center;
      line-height: 40px;
      user-select: none;
      letter-spacing: 8px;
      font-weight: 700;
      color: whitesmoke;
    }
    .el-menu {
      border-right: 0px;
    }
    .iconfont {
      margin-right: 10px;
    }
    background-color: #515458;
  }
  .el-main {
    background-color: #fffffffd;
  }
}
</style>
